package com.dragon.gencode.conf.db;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;

/**
 * 该类继承自 AbstractRoutingDataSource 类，
 * 在访问数据库时会调用该类的 determineCurrentLookupKey() 方法获取数据库实例的 key
 */
public class DynamicRoutingDataSource extends AbstractRoutingDataSource {
 
	private static final Logger logger = LoggerFactory.getLogger(DynamicRoutingDataSource.class);
	
	@Override
	protected Object determineCurrentLookupKey() {
		final Object dataSourceKey = DynamicDataSourceContextHolder.getDataSourceKey();
		logger.info("current datasource is : {}", dataSourceKey);
		return dataSourceKey;
	}
}